"use strict";
/* Controllers */
app.controller("etpReportController", ['$scope', '$location', '$rootScope', '$routeParams', 'etpReportService', function ($scope, $location, $rootScope, $routeParams, etpReportService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};

    if (vm.p.biz == null || vm.p.biz == '0') {
        vm.p.biz = 'ganji';
    }

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if ($scope.vm.p.bizTime == '' || typeof ($scope.vm.p.bizTime) == 'undefined') {
        $scope.vm.p.bizTime = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    var errorFn = function (result) {
        alert(result);
    };
    $scope.queryReport = function () {
        $scope.listEtpReport();
    };
    $scope.listEtpReport = function () {
        $("#refreshTable").addClass('disabled');
        $("#list_table").bootstrapTable('showLoading')
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = vm.p
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            $('#list_table').bootstrapTable('hideLoading');
            $("#refreshTable").removeClass('disabled');
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);

        };
        etpReportService.listEtpReport(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listEtpReport();
    };

    $scope.initializeController();


    $scope.toMetricChart = function (field, adEtpname, adDisptype) {
        var dom = document.getElementById('metric_chart');
        var myChart = echarts.init(dom);
        myChart.showLoading()

        var params = {};
        params.field = field
        params.adEtpname = adEtpname
        params.adDisptype = adDisptype
        var procFn = function (response) {
            vm.result = response;
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            let modalTitle = '企业名：' + adEtpname + '商业类型：' + adDisptype
            $scope.vm.modalTitle = modalTitle
            initChart(myChart, data.chartData, field)
            myChart.hideLoading()
        };
        etpReportService.getEtpChart(params, procFn, errorFn);

        $('#myModal').modal('show')
    }

    function initChart(myChart, data, title) {

        var dateList = data.map(function (item) {
            return item[0];
        });
        var valueList = data.map(function (item) {
            return item[1];
        });
        // var dom = document.getElementById("container");

        window.onresize = function () {
            myChart.resize();
        };
        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { type: 'cross' }
            },
            legend: {
            },
            dataZoom: [{}],
            xAxis: {
                type: 'category',
                data: dateList
            },
            yAxis: [
                {
                    name: '指标值',
                    type: 'value'
                }
            ],
            series: [
                {
                    name: title,
                    yAxisIndex: 0,
                    data: valueList,
                    type: 'line',
                    smooth: true
                }
            ]
        };
        window.onresize = function () {
            myChart.resize();
        };
        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

}]);


/* Services */
app.service('etpReportService', ['ajaxService', function (ajaxService) {

    this.listEtpReport = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/report/listEtpReport.do", successFunction, errorFunction);
    };

    this.getEtpChart = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/report/getEtpChart.do", successFunction, errorFunction);
    };
}]);

function userReportFormatter(value, row, index, field) {
    let clickFncStr = "angular.element(this).scope().toMetricChart('" + field + "', '" + row.adEtpname + "', '" + row.adDisptype + "')"
    return [
        '<a href="javascript:void(0)" onclick="' + clickFncStr + '">',
        numberFmt(value),
        '</a>  ',
    ].join('')
}


